<?php

namespace App\Http\Controllers;

use App\Events\ScoreChangedEvent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }

    public function score(Request $request)
    {
        $credential = $request->validate(['team' => 'required', 'type' => 'numeric|required']);
        $credential['type'] ? $credential['score'] = Redis::incr('team-'.$credential['team']) : $credential['score'] = Redis::decr('team-'.$credential['team']);
        unset($credential['type']);
        broadcast(new ScoreChangedEvent($credential));
    }
}
